<!DOCTYPE html>
<html>
<head>
	<script type="text/javascript" src="__STATIC__/common/js/jquery.min.js"></script>
</head>
<body>
<div id="box-main">
	<h3>货币设置</h3>
	<div id="coinConfig">
		<form>
		</form>
		<input type="button" onclick="saveCoinConfig()" value="提交保存">
	</div>
	<h3>奖金设置</h3>
	<div id="bonusConfig">
		<form>
			{volist name="$data.levels" id="level"}
			<div class="item">
				<p>【{$level.level_name}】</p>
				<p class="loop">倍数-<input name="multiple" value="0"> 比率-<input name="rate" value="20"><input name="level_id" type="hidden" value="{$level.id}"></p>
				<p class="loop">倍数-<input name="multiple" value="1"> 比率-<input name="rate" value="15"><input name="level_id" type="hidden" value="{$level.id}"></p>
				<p class="loop">倍数-<input name="multiple" value="5"> 比率-<input name="rate" value="10"><input name="level_id" type="hidden" value="{$level.id}"></p>
			</div>
			{/volist}
		</form>
		<input type="button" onclick="saveBonusConfig()" value="提交保存">
	</div>
</div>

<script type="text/javascript">
var coins = {$data['coins']|json_encode};
$(function(){
	getCoinConfig();
	getBonusConfig();
})

/**
 * 获取货币配置数据
 * @return {[type]} [description]
 */
function getCoinConfig(){
	$.ajax({
		'type':'post',
		'dataType':'json',
		'url':'{:url('plugin.api/plugin_config')}',
		'data':'plugin={$data['plugin_dir']}&config=coin',
		'success':function(data){
			var html = '';
			$.each(data.config_value,function(index,value){
				html += '<div class="item">货币类型<select name="coin_id">{volist name="$data['coins']" id="coin"}<option value="{$coin.id}">{$coin.coin_name}</option>{/volist}</select>配置<input type="text" name="config" value="'+value.config+'"></div>';
			})
			$("#coinConfig form").html(html);
		}
	});
}

/**
 * 获取奖金配置
 * @return {[type]} [description]
 */
function getBonusConfig(){
	$.ajax({
		'type':'post',
		'dataType':'json',
		'url':'{:url('plugin.api/plugin_config')}',
		'data':'plugin={$data['plugin_dir']}&config=bonus',
		'success':function(data){
			
		}
	});
}


/**
 * 保存货币配置
 * @return {[type]} [description]
 */
function saveCoinConfig(){

	var array = [];

	$("#coinConfig .item").each(function(index){
		var coin_id = $(this).find("select[name=coin_id]").val();
		var config = $(this).find("input[name=config]").val();
		var data = {
			"coin_id" : coin_id,
			"config" : config 
		};
		array.push(data);

	});

	$.ajax({
		'type':'post',
		'dataType':'json',
		'url':'{:url('plugin.handle/plugin')}',
		'data':{
			"name":"{$data['plugin_dir']}",
			"action":"saveCoinConfig",
			"data":array
		},
		'success':function(data){

		}
	});
}

/**
 * 保存奖金配置
 * @return {[type]} [description]
 */
function saveBonusConfig(){

	var array = [];

	$("#bonusConfig .item").each(function(index){
		array[index] = [];
		$(this).find("p.loop").each(function(i){
			var level_id = $(this).find("input[name=level_id]").val();
			var multiple = $(this).find("input[name=multiple]").val();
			var rate = $(this).find("input[name=rate]").val();
			var data = {
				"level_id" : level_id,
				"multiple" : multiple,
				"rate" : rate
			};
			array[index][i] = data;
		});

	});

	$.ajax({
		'type':'post',
		'dataType':'json',
		'url':'{:url('plugin.handle/plugin')}',
		'data':{
			"name":"{$data['plugin_dir']}",
			"action":"saveBonusConfig",
			"data":array
		},
		'success':function(data){

		}
	});
}
</script>
</body>
</html>